package org.dfrt.own.controller;


import org.dfrt.own.entity.User;
import org.dfrt.own.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * @author zhang
 * ClassName: UserController
 * Description: 用户管理控制器
 * date: 2023/10/3 20:16
 * @since JDK 8
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 用户登录
     *
     * @param uname
     * @param upasswd
     * @param session
     * @return
     */
    @RequestMapping("/login")
    public String login(String uname, String upasswd, HttpSession session, HttpServletRequest request) {


        //如果验证码正确则进行以下步骤
        User user = userService.selectUserToLogin(uname, upasswd);
//        System.out.println(user);
        if (user != null) {
            //用户存在
            session.setAttribute("user", user);

            //重定向到首页
            return "redirect:/main.jsp";
        } else {
            Map<String, String> msgmap = new HashMap<>();
            msgmap.put("sta", "no");
            msgmap.put("msg", "用户名或者密码错误");
            request.setAttribute("msgmap", msgmap);
            return "login";
        }
    }

    /**
     * 用户注册
     *
     * @param uname
     * @param passwd
     * @param repasswd
     * @param request
     * @return
     */
    @RequestMapping("/register")
    public String register(String uname, String passwd, String repasswd, HttpServletRequest request) {
        Map<String, String> msgmap = userService.registerUser(uname, passwd, repasswd);

        request.setAttribute("msgmap", msgmap);

        return "login";
    }

    /**
     * 修改用户信息
     * @param uname
     * @param ubirthday
     * @param udesc
     * @param upasswd
     * @param session
     * @param request
     * @return
     */
    @RequestMapping("/update")
    public String update(String uname, String ubirthday, String udesc, String upasswd, HttpSession session, HttpServletRequest request) {
        User user = (User) session.getAttribute("user");

        Map<String,String> msgmap = userService.updateUser(session ,user.getUid(),uname,ubirthday,udesc,upasswd);

        request.setAttribute("msgmap",msgmap);
//        if (msgmap.get("uu")!=null){
//            return "forward:/login.jsp";
//        }
//        System.out.println(msgmap.get("sta")+"  "+ msgmap.get("msg"));
        return "updateUser";
    }

    /**
     * 退出登录
     * @param session
     * @return
     */
    @RequestMapping("/logout")
    public String logout( HttpSession session){
        session.removeAttribute("user");
        return "toLogin";
    }

    /**
     * 注销用户
     * @param request
     * @return
     */
    @RequestMapping("/delet")
    public String delet(HttpServletRequest request,HttpSession session){
        Integer uid =Integer.parseInt(request.getParameter("uid"));

        User user = new User();
        user.setUid(uid);

        session.removeAttribute("user");

        Map<String,String> msgmap = userService.deletUserByUser(user);

        request.setAttribute("msgmap",msgmap);

        return "toLogin";
    }

}
